const CryptoJS = require("crypto-js");
const axios = require(`axios`)

function getSign(now) {
    // 数据
    const data = `73b34182aaed4559b56e5504801f557b|4d3fdf97a5424568ae421d876d4cd5e5|4bd4ba737214495db86869b1d40e9552|ZH202012300001|T0002|${now}`;
    // 密钥
    const key = '6e4d4152306744616873434157757a5a46476c70727654304a4b326a68453549';
    const pKey = CryptoJS.enc.Hex.parse(key)
    const encrypted = CryptoJS.AES.encrypt(data, pKey, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7,
    });
    return encrypted.ciphertext.toString()
}

async function buy(token) {
    const now = new Date().getTime()
    const sign = getSign(now)

    return await axios.post(`https://vtravel.link2shops.com/vfuliApi/api/client/ypJyOrder/submitWithSign`, {
        "pageTag": "szcr",
        "openId": null,
        "activityId": "4d3fdf97a5424568ae421d876d4cd5e5",
        "goodsId": "4bd4ba737214495db86869b1d40e9552",
        "channelId": "73b34182aaed4559b56e5504801f557b",
        "rechargeAccount": "",
        "rechargeType": "1",
        "notifyMobile": "18336421281",
        "merchantCode": "ZH202012300001",
        "platformTp": "T0002",
        "bankType": "CCB",
        "cardBin": "489592",
        "version": "V2.0.0",
        "sourceTp": "H5",
        "timestamp": now,
        "memberName": "",
        "memberAccount": "",
        "sign": sign,
        "tertiarySource": "02",
        "homeUrl": "https://vtravel.link2shops.com/yiyuan/#/",
        "channelDistinct": ""
    }, {
        headers: {
            Accept: "application/json, text/plain, */*",
            "Content-Type": "application/json",
            Host: "vtravel.link2shops.com",
            Origin: "https://vtravel.link2shops.com",
            Referer: "https://vtravel.link2shops.com/yiyuan/?uuid=B6uu8DDpWIrA2RmtWjFrxgWhIezW+GuoHrlF/P2FV0g=",
            "User-Agent": "Mozilla/5.0 (Linux; Android 10; SM-G981B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Mobile Safari/537.36 Edg/117.0.0.0",
            token: token

        }
    })
}

function main() {
    // token 信息
    const token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIyZDBiNDMwZDgyMjY0MmRlYjc3NjQzMjE0MTdmODZhYS1mYjM0OTEwZjQ3NzJmYTdiMzRlMWM0MjU3ZTZiNTQ4NiIsImlhdCI6MTY5NTU1NjEzNywiZXhwIjoxNzAzMzMyMTM3fQ.Dfxp3aoqncB2wNt0050P3WDP29ULFecOfCJMuToH0ZmhlkRYDbVLNrnAaCWoA7krb1uOaY-Rm-2Rtq1AxSv5sA'
    // 延迟
    const delayTime = 1000;
    let i = 1
    setInterval(async () => {
        const resp = await buy(token)
        console.log(`第 ${i++} 次抢购结果`, resp.data)
    }, delayTime)
}
main()